package behavior;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;


public class UploadFileSteps {

    @Given("you can upload file")
    public static void uploadFile(){
        // 设置 ChromeDriver 路径
//        System.setProperty("webdriver.chrom.driver", "D:\\softwares\\allure\\allure\\bin/chromedriver.exe");
        System.setProperty("webdriver.chrom.driver", "src/test/resources/chromedriver.exe");

        // 初始化 WebDriver
        WebDriver driver = new ChromeDriver();
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        JavascriptExecutor js = (JavascriptExecutor) driver;

        try {
            // 打开目标页面
            driver.get("http://47.107.116.139/phpwind/");
            driver.manage().window().maximize();
            driver.findElement(By.xpath("//*[text()='登录']")).click();
//            driver.findElement(By.xpath("//*[@id='J_u_login_username']")).sendKeys("611");
            WebElement userNameEl = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='J_u_login_username']")));
//            js.executeScript("arguments[0].click();", userNameEl);
//            userNameEl.sendKeys("611");
            js.executeScript("arguments[0].click();", userNameEl);
            js.executeScript("arguments[0].value = arguments[1];", userNameEl, "611");

//            driver.findElement(By.xpath("//*[@id='J_u_login_password']")).sendKeys("123456");
            WebElement pwsNameEl = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='J_u_login_password']")));
//            js.executeScript("arguments[0].click();", pwsNameEl);
            js.executeScript("arguments[0].value = arguments[1];", pwsNameEl, "123456");

//            pwsNameEl.sendKeys("123456");
            driver.findElement(By.xpath("//*[@id=\"J_u_login_form\"]/div/dl[4]/dd/button")).click();

//            driver.findElement(By.xpath("//*[@id=\"J_head_user_a\"]/em")).click();
            WebElement rowEl = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"J_head_user_a\"]/em")));
            js.executeScript("arguments[0].click();", rowEl);


            driver.findElement(By.xpath("//*[@id=\"J_head_user_menu\"]/div/ul[1]/li[1]/a")).click();
            driver.findElement(By.xpath("/html/body/div/div[2]/div[1]/div[3]/ul/li[3]/a")).click();
            driver.findElement(By.xpath("/html/body/div/div[2]/div[2]/div[1]/div/div[2]/h3/a")).click();
            driver.findElement(By.xpath("//*[@id=\"profile_avatar\"]")).click();


//            driver.get("http://47.107.116.139/phpwind/index.php?m=profile&c=avatar&_left=avatar");
//            driver.findElement(By.xpath("//*[text()='普通上传模式']")).click();
            WebElement uploadBtn = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[text()='普通上传模式']")));
            uploadBtn.click();
            WebElement upBtn = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@name='avatar']")));
            js.executeScript("arguments[0].click();", upBtn);
//            Thread.sleep(5000);
            // 点击文件上传按钮（假设按钮的 ID 是 "uploadButton"）
//            WebElement uploadButton = driver.findElement(By.id("uploadButton"));
//            uploadButton.click();

            // 文件路径
            String filePath = "E:\\software\\autoit\\autoIt_demo\\qqq.png";

            // AutoIT 可执行文件路径
            String autoItScriptPath = "E:\\software\\autoit\\autoIt_demo\\FileUpload.exe";

            // 调用 AutoIT 脚本
            ProcessBuilder processBuilder = new ProcessBuilder(autoItScriptPath, filePath);
            Process process = processBuilder.start();
            process.waitFor(); // 等待脚本执行完成

            System.out.println("----------------------------File upload completed!------------------------------------------------------------------------");
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        } finally {
            // 关闭浏览器
//            driver.quit();
        }

    }

}
